package fr.ensisa.sunspot.actor.client;

import fr.ensisa.sunspot.actor.AbstractActor;
import fr.ensisa.sunspot.actor.IActor;
import fr.ensisa.sunspot.actor.Template;
import fr.ensisa.sunspot.actor.client.mouse.Mouse_simulator;
import fr.ensisa.sunspot.event.AbstractEvent;
import fr.ensisa.sunspot.event.DoubleIntegerEvent;
import fr.ensisa.sunspot.event.IEvent;
import fr.ensisa.sunspot.event.IntegerEvent;
import fr.ensisa.sunspot.event.MessageEvent;
import fr.ensisa.sunspot.event.MouseEvent;
import fr.ensisa.sunspot.event.TemplateEvent;

public class MouseActor extends AbstractActor {
	
	private Mouse_simulator mouse_simulator;

	public MouseActor() {
		super(IActor.KIND_WRITER);
		setTemplate(this.BuildTemplate());
	}

	private Template BuildTemplate() {
		Template template = new Template();
		TemplateEvent event = new TemplateEvent(IEvent.MAJOR_MOUSE, IEvent.MINOR_NO, IEvent.STATE_EMPTY);
		template.addEvent(event);
		return template;
	}

	@Override
	public int getID() {
		return 16;
	}
	
	private Mouse_simulator getMouse_simulator() {
		if (mouse_simulator==null) mouse_simulator = new Mouse_simulator();
		return mouse_simulator;
	}

	@Override
	public int process(long timeStamp, IEvent event) {
		if (! (event instanceof AbstractEvent)) return IActor.RETURN_DONOTHING;
		switch (event.getMinor()) {
		case IEvent.MINOR_Mouse_event:
			this.Exec_Mouse_event((MouseEvent) event);
			break;
		case IEvent.MINOR_PostMessage:
			this.Exec_PostMessage((MessageEvent) event);
			break;
		case IEvent.MINOR_SendMessage:
			this.Exec_SendMessage((MessageEvent) event);
			break;
		case IEvent.MINOR_SetCursorPos:
			this.Exec_SetCursorPos((DoubleIntegerEvent) event);
			break;
		case IEvent.MINOR_CursorUp:
			this.Exec_CursorUp((IntegerEvent) event);
			break;
		case IEvent.MINOR_CursorDown:
			this.Exec_CursorDown((IntegerEvent) event);
			break;
		case IEvent.MINOR_CursorLeft:
			this.Exec_CursorLeft((IntegerEvent) event);
			break;
		case IEvent.MINOR_CursorRight:
			this.Exec_CursorRight((IntegerEvent) event);
			break;
		}
		return IActor.RETURN_DONOTHING;
	}

	private void Exec_CursorRight(IntegerEvent event) {
		getMouse_simulator().CursorRight(event.getIntensity());
	}

	private void Exec_CursorLeft(IntegerEvent event) {
		getMouse_simulator().CursorLeft(event.getIntensity());
	}

	private void Exec_CursorDown(IntegerEvent event) {
		getMouse_simulator().CursorDown(event.getIntensity());
	}

	private void Exec_CursorUp(IntegerEvent event) {
		getMouse_simulator().CursorUp(event.getIntensity());
	}

	private void Exec_SetCursorPos(DoubleIntegerEvent event) {
		getMouse_simulator().SetCursorPos(event.getX(), event.getY());
	}

	private void Exec_SendMessage(MessageEvent event) {
		getMouse_simulator().SendMessage(event.getWindow(), event.getUint(), event.getBvk(), event.getI());
	}

	private void Exec_PostMessage(MessageEvent event) {
		getMouse_simulator().PostMessage(event.getWindow(), event.getUint(), event.getBvk(), event.getI());
		
	}

	private void Exec_Mouse_event(MouseEvent event) {
		getMouse_simulator().Mouse_event(event.getDwEvents(), event.getX(), event.getY(), event.getDwData());		
	}

}
